var ReservationsNoArrivedTable = {
    id: "ReservationsNoArrivedTable",
    seItem: null,
    table: null,
    layerIndex: -1
};
function _formatterTime(value, row, index) {
    if (value !== null && value !== "") {
        return value.substring(0, 19);
    } else {
        return "";
    }
}

function _formatterTitle(value, row, index) {
    if (value !== null && value !== "") {
        return "<span style=' cursor:pointer' title='" + row.huifang + "' onclick='ReservationsNoArrivedTable.reservationsNotDetail(" + '"' + row.id + '"' + ")'>" + value.substring(0, 19) + "</span>"
    } else {
        return "";
    }
}

ReservationsNoArrivedTable.initColumn = function () {
    var columns =
        [
            {field: 'selectItem', radio: true},
            {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
            {title: '预约号', field: 'reservationsNum', align: 'center', valign: 'middle', width: 110, sortable: true},
            {title: '姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
                formatter: function (value, row, index) {
                    return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ReservationsNoArrivedTable.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='"+ row.customerSexHead +"' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }
            },
            {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140},
            {title: '预约门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 110},
            {title: '回访状态', field: 'statuss', align: 'center', valign: 'middle', width: 110, sortable: true},
            {title: '预约时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    if (row.huifang === "" || row.huifang == null) {
                        row.huifang = "无回访详情";
                    } else {
                        row.huifang = ",<b>" + "回访详情：" + row.huifang.replace(/\s{2,}/g, "");
                        var reg = /,<b>/g;
                        var regs = /<\/b>/g;
                        row.huifang = row.huifang.replace(reg, '&#10;');
                        row.huifang = row.huifang.replace(regs, ' ');
                    }
                    return _formatterTitle(value, row, index);
                }
            },
            {title: '建档人', field: 'archiving', align: 'center', valign: 'middle', sortable: true, width: 90},
            {title: '跟进人', field: 'follower', align: 'center', valign: 'middle', sortable: true, width: 90},
            {title: '建档时间', field: 'filingTime', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    return _formatterTime(value, row, index);
                }
            },
            {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true, width: 110},
            {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true, width: 110},
            {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true, width: 130},
            {title: '回访', field: 'huifang', align: 'center', valign: 'middle', width: 250,
                formatter: function (value, row, index) {
                    return '<span title="'+value+'">'+value+'</span>'
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow":"hidden","white-space":"nowrap","text-overflow":"ellipsis"}}
                }
            },
            {title: '性别', field: 'customerSex', align: 'center', valign: 'middle', width: 80, sortable: true,
                formatter: function (value, row, index) {
                    return value === 1 ? '男' : value === 2 ? '女' : '未知';
                }
            },
            {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80},
            {title: '预约病种',field: 'appointmentPayService',align: 'center',valign: 'middle',sortable: true,width: 110},
            {title: '访问病种', field: 'accessDisease', align: 'center', valign: 'middle', sortable: true, width: 110},
            {title: '关键词', field: 'keyWord', align: 'center', valign: 'middle', sortable: true, width: 100},
            {title: '微信号', field: 'wechatNumber', align: 'center', valign: 'middle', sortable: true, width: 100},
        ];
    return columns;
};
ReservationsNoArrivedTable.search = function () {//查询预约客户列表
    ReservationsNoArrivedTable.table.refresh({query: ReservationsNoArrivedTable.formParams()});
    // ReservationsNoArrivedTable.tongji();
};
ReservationsNoArrivedTable.formParams = function () {//新市场预约表单查询提交参数
    var queryData = {};
    queryData['outpatientName'] = multipleSelection($('#outpatientName').val());  //门诊
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    queryData['department'] = $("#department").val(); //  部门
    queryData['reach'] = $("#marketInputPublic", parent.document).val();  //姓名   电话   微信  预约号
    queryData['appointmentPayService'] = $("#appointmentPayService").val();  //预约项目
    queryData['archiving'] = $("#archiving").val();   //建档人
    queryData['follower'] = $("#follower").val();
    queryData['accessDisease'] = $("#accessDisease").val();  //访问病种
    queryData['oneChannel'] = $("#oneChannel").val();  //一级渠道
    queryData['twoChannel'] = $("#twoChannel").val();    //二级渠道
    queryData['thrChannel'] = $("#thrChannel").val();    //三级渠道
    queryData['statuss'] = multipleSelection($('#statuss').val());//回访状态
    queryData['status'] = $("#status").val();   //状态
    queryData['startFilingTime'] = $("#startFilingTime").val();   //建档时间
    queryData['endFilingTime'] = $("#endFilingTime").val();   //
    queryData['startAppointmentTime'] = $("#startAppointmentTime").val();  //建档时间
    queryData['endAppointmentTime'] = $("#endAppointmentTime").val();
    queryData['area'] = multipleSelection($('#area').val());
    return queryData;
};
var parseParam = function (param) {//把对象拼接成URL的键值对参数 param1=value1&param2=value2
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {
    return key + "=" + param[key];
};
ReservationsNoArrivedTable.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ReservationsNoArrivedTable.seItem = selected[0];
        return true;
    }
};
// ReservationsNoArrivedTable.tongji = function () {//统计建档未到人数
//     var param1 = multipleSelection($('#outpatientName').val());  //门诊
//     var param2 = $("#department").val();   //部门
//     var param3 = $("#appointmentPayService").val(); //预约病种
//     var param4 = $("#archiving").val();  //建档人
//     var param5 = $("#follower").val();  //跟进人
//     var param6 = $("#accessDisease").val();  //访问病种
//     var param7 = $("#oneChannel").val();  //一级渠道
//     var param8 = $("#twoChannel").val();  //二级渠道
//     var param9 = $("#thrChannel").val();  //三级渠道
//     var param10 = multipleSelection($('#statuss').val()); //跟踪状态
//     var param11 = $("#startFilingTime").val();  //三级渠道
//     var param12 = $("#endFilingTime").val(); //跟踪状态
//     var date = new Date();
//     var currentWeek = date.getDay();
//     var stepSunDay = -currentWeek + 1;
//     if (currentWeek == 0) {stepSunDay = -7;}
//     var stepMonday = 7 - currentWeek;
//     var time = date.getTime();
//     var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
//     var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
//     var param13 = setDateTime(monday,0) + " " + "00:00:00";
//     var param14 = setDateTime(sunday,0) + " " + "23:59:59";
//     var param15 = multipleSelection($('#area').val());
//     var param16 =  $("#marketInputPublic", parent.document).val();  //姓名   电话   微信  预约号
//     $.ajax({
//         type: "GET",
//         url: Feng.ctxPath + "/reservationsNotArrive/tongji",
//         data: {
//             param1: param1, param2: param2, param3: param3,
//             param4: param4, param5: param5, param6: param6,
//             param7: param7, param8: param8, param9: param9,
//             param10: param10, param11: param11,param12: param12,
//             param13: param13,param14: param14,param15: param15,param16: param16
//         },
//         dataType: "json",
//         success: function (data) {
//             $("#weidaoToday").html(data[0].countNum);
//             $("#weidaoYesterday").html(data[1].countNum);
//             $("#weidaoThisWeek").html(data[2].countNum);
//             $("#weidaoMonth").html(data[3].countNum);
//             $("#weidaoLastMonth").html(data[4].countNum)
//         }
//     });
// };
ReservationsNoArrivedTable.reservationsNotDetail = function (id) {//点击表格姓名进入详情 - 未到
    var pageType = $("#pageType").val();
    window.localStorage.setItem("c", pageType);
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/reservationsNotArrive/to_reservationsNotDetail/' + id
    });
    this.layerIndex = index;
};
ReservationsNoArrivedTable.getData = function () {//片区筛选
    var area = multipleSelection($('#area').val());
    $.post(Feng.ctxPath + "/dept/getListByArea/"+area,function (data) {
        $('#outpatientName').empty();
        for (var i = 0; i < data.length; i++) {
            $('#outpatientName').append("<option value=" + data[i].simplename + ">" + data[i].simplename + "</option>");
        }
        $('#outpatientName').selectpicker('refresh');
        $('#outpatientName').selectpicker('render');
        ReservationsNoArrivedTable.search();
    }, "json");
};
ReservationsNoArrivedTable.getFollower = function () {//选择跟进人
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};
ReservationsNoArrivedTable.getArchiving = function () {//选择建档人
    var index = layer.open({
        type: 2,
        title: '选中建档人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservation/to_archiving'
    });
    this.layerIndex = index;
};
ReservationsNoArrivedTable.reservationsExportWD = function () {//导出
    var param = parseParam(ReservationsNoArrivedTable.formParams());
    location.href = Feng.ctxPath + '/reservationsNotArrive/reservationsExportSCWD?' + param;
};
ReservationsNoArrivedTable.resetSearch = function () {//重置
    $("#marketInputPublic", parent.document).val("");
    $("#archiving").val("");
    $("#follower").val("");
    $("#accessDisease").val("");
    $("#statuss").selectpicker('deselectAll');
    $("#startAppointmentTime").val("");
    $("#endAppointmentTime").val("");
    $("#startFilingTime").val("");
    $("#endFilingTime").val("");
    $("#appointmentPayService").val("");
    $("#oneChannel").val("请选择");
    $("#twoChannel").val("请选择");
    $("#thrChannel").val("请选择");
    $("#area").selectpicker('deselectAll');
    $("#outpatientName").selectpicker('deselectAll');
    $("#department").val("");
    ReservationsNoArrivedTable.search();
};
ReservationsNoArrivedTable.today = function () {//点击上面状态筛选表格
    $("#startAppointmentTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
    $("#endAppointmentTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    ReservationsNoArrivedTable.search();
};
ReservationsNoArrivedTable.yesterday = function () {
    $("#startAppointmentTime").val(setDateTime(new Date(),1) + " " + "00:00:00");
    $("#endAppointmentTime").val(setDateTime(new Date(),1) + " " + "23:59:59");
    ReservationsNoArrivedTable.search();
};
ReservationsNoArrivedTable.thisWeek = function () {
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {stepSunDay = -7;}
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    $("#startAppointmentTime").val(setDateTime(monday,0) + " " + "00:00:00");
    $("#endAppointmentTime").val(setDateTime(sunday,0) + " " + "23:59:59");
    ReservationsNoArrivedTable.search();
};
ReservationsNoArrivedTable.thisMonth = function () {
    $("#startAppointmentTime").val(firstMonthTime(new Date(),0) + " " + "00:00:00");
    $("#endAppointmentTime").val(setMonthTime(new Date(),0) + " " + "23:59:59");
    ReservationsNoArrivedTable.search();
};
ReservationsNoArrivedTable.lastMonth = function () {
    $("#startAppointmentTime").val(firstMonthTime(new Date(),1) + " " + "00:00:00");
    $("#endAppointmentTime").val(setMonthTime(new Date(),1) + " " + "23:59:59");
    ReservationsNoArrivedTable.search();
};
$("#marketSearchAll", parent.document).click(function(){//主页搜索
    ReservationsNoArrivedTable.search();
});
function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#ReservationsNoArrivedTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
var scollPostion = 0;
ReservationsNoArrivedTable.getScrollPosition = function (){//获取表格滚动到什么地方
    scollPostion = $('#ReservationsNoArrivedTable').bootstrapTable('getScrollPosition');
};
ReservationsNoArrivedTable.scrollTo = function (){//指定表格滚动到什么地方
    $("#ReservationsNoArrivedTable").on('load-success.bs.table',function(data){
        $('#ReservationsNoArrivedTable').bootstrapTable('scrollTo', scollPostion);
    });
};
$(function () {
    var defaultColunms = ReservationsNoArrivedTable.initColumn();
    var table = new BSTable(ReservationsNoArrivedTable.id, "/reservationsNotArrive/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ReservationsNoArrivedTable.formParams());
    ReservationsNoArrivedTable.table = table.init();
    ReservationsNoArrivedTable.today();
    menuNameHideColumn($("#menuName").val(),'ReservationsNoArrivedTable');
    $("#today").addClass('actionClick').siblings().removeClass('actionClick');
    $(document).on('click','#laydate_clear,#laydate_today,#laydate_ok',function () {ReservationsNoArrivedTable.search();});
    $("#oneChannel").change(function () {$("#twoChannel").val('请选择');$("#thrChannel").val('请选择');ReservationsNoArrivedTable.search();});
    $("#twoChannel").change(function () {$("#thrChannel").val('请选择');ReservationsNoArrivedTable.search();});
    $("#thrChannel").change(function () {ReservationsNoArrivedTable.search();});
    checkDate = function () {ReservationsNoArrivedTable.search();};
    callbackTable = function () {ReservationsNoArrivedTable.search();};
    phonePrivileges();
});